//
//  LGLungFollowEditController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2024/7/5.
//  肺癌-随访计划-编辑随访计划

#import "LGLungFollowEditController.h"
//选择项目、评估、宣教、
#import "LGLungFollowProjectController.h"
#import "LGLungFollowEditTaskHeader.h"
#import "LGLungFollowEditTaskCell.h"
#import "LGPickerView.h"
@interface LGLungFollowEditController ()<UITableViewDelegate,UITableViewDataSource,LGBottomButtonDelegate,LGLungFollowEditTaskDelegate,LGLungFollowEditTaskHeaderDelegate>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)LGBottomButtonView *bottomView;
/**   */
@property (nonatomic , strong)NSArray *typeArray;
/**   */
@property (nonatomic , strong)NSArray *typeNameArray;

@end

@implementation LGLungFollowEditController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"编辑随访计划";
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.bottomView];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/select/careplantypeSelect" parameters:@{} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.typeArray = returnData;
            NSMutableArray *tempArray = [NSMutableArray array];
            for (NSDictionary *tempDic in self.typeArray) {
                [tempArray addObject:LGNSString(tempDic[@"title"])];
            }
            self.typeNameArray = tempArray;
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    [self.tableView reloadData];
}

//新增随访计划
-(void)addFollowUpAction{
    NSMutableArray *tempArray = [NSMutableArray arrayWithArray:self.dataArray];
    NSDictionary *tempDic = @{@"task_type":@"",@"task_type_name":@"",@"days":@"",@"exams":@[],@"evas":@[],@"preachs":@[]};
    [tempArray addObject:tempDic];
    self.dataArray = tempArray;
}

//删除随访计划
-(void)deleteFollowUp:(NSInteger)section{
    NSMutableArray *tempArray = [NSMutableArray arrayWithArray:self.dataArray];
    [tempArray removeObjectAtIndex:section-1];
    self.dataArray = tempArray;
}

//编辑天数
-(void)editTaskDays:(NSString *)days section:(NSInteger)section{
    NSMutableArray *tempArray = [NSMutableArray arrayWithArray:self.dataArray];
    NSMutableDictionary *sectionDic = [NSMutableDictionary dictionaryWithDictionary:tempArray[section-1]];
    [sectionDic setValue:days forKey:@"days"];
    [tempArray replaceObjectAtIndex:section-1 withObject:sectionDic];
    self.dataArray = tempArray;
}

//选择类型
-(void)selectTaskType:(NSInteger)section{
    [self.view endEditing:YES];
    if (self.typeArray.count == 0) {
        [self requestData];
    }else{
        LGPickerView *pickerView = [[LGPickerView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
        pickerView.pickArry = @[self.typeNameArray];
        pickerView.titleLabel.text = @"选择随访类型";
        [[ObjectTool mainWindow] addSubview:pickerView];
        __block NSInteger editIndex = section-1;
        __weak typeof(self) weakSelf = self;
        pickerView.selectPickIndexResult = ^(NSArray *resultArray, NSArray *indexArray) {
            NSInteger index = [indexArray[0] integerValue];
            NSDictionary *typeDic = self.typeArray[index];
            NSMutableArray *tempArray = [NSMutableArray arrayWithArray:weakSelf.dataArray];
            NSMutableDictionary *sectionDic = [NSMutableDictionary dictionaryWithDictionary:tempArray[editIndex]];
            [sectionDic setValue:LGNSString(typeDic[@"title"]) forKey:@"task_type_name"];
            [sectionDic setValue:LGNSString(typeDic[@"value"]) forKey:@"task_type"];
            [tempArray replaceObjectAtIndex:editIndex withObject:sectionDic];
            self.dataArray = tempArray;
        };
    }
}


//添加/编辑：随访项目、评估量表、宣教项目
-(void)editTaskAction:(NSString *)action title:(NSString *)title dataDic:(NSDictionary *)dataDic key:(NSString *)key indexPath:(NSIndexPath *)indexPath{
    [self.view endEditing:YES];
    __block NSInteger section = indexPath.section;
    NSArray *tempArray = self.dataArray[section][key];
    LGLungFollowProjectController *controller = [[LGLungFollowProjectController alloc]init];
    controller.action = action;
    controller.title = title;
    controller.dataDic = (tempArray.count>0)?tempArray[0][@"codes"]:@{@"codes":@[],@"names":@[]};
    [self.navigationController pushViewController:controller animated:YES];
    __block NSString *dataKey = key;
    __weak typeof(self) weakSelf = self;
    controller.selectProjectSuccess = ^(NSDictionary * _Nonnull selectDic) {
        NSMutableArray *tempArray = [NSMutableArray arrayWithArray:self.dataArray];
        NSMutableDictionary *sectionDic = [NSMutableDictionary dictionaryWithDictionary:tempArray[section]];
        NSMutableArray *sectionArray = [NSMutableArray arrayWithArray:sectionDic[dataKey]];
        if (sectionArray.count>0) {
            NSMutableDictionary *rowDic = [NSMutableDictionary dictionaryWithDictionary:sectionArray[0]];
            [rowDic setValue:selectDic forKey:@"codes"];
            [sectionArray replaceObjectAtIndex:0 withObject:rowDic];
        }else{
            [sectionArray addObject:@{@"codes":selectDic,@"keywords":@[]}];
        }
        [sectionDic setValue:sectionArray forKey:dataKey];
        [tempArray replaceObjectAtIndex:section withObject:sectionDic];
        weakSelf.dataArray = tempArray;
    };
}

//关键字-某基线下的项目、评估、宣教
-(void)editKeywords:(NSDictionary *)dataDic indexPath:(NSIndexPath *)indexPath{
    NSMutableArray *tempArray = [NSMutableArray arrayWithArray:self.dataArray];
    [tempArray replaceObjectAtIndex:indexPath.section withObject:dataDic];
    self.dataArray = tempArray;
}

//删除-某基线下的项目、评估、宣教 -- 暂时废除
-(void)deleteTaskAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    NSMutableArray *tempArray = [NSMutableArray arrayWithArray:self.dataArray];
    NSMutableDictionary *sectionDic = [NSMutableDictionary dictionaryWithDictionary:tempArray[section]];
    NSMutableArray *sectionArray = [NSMutableArray arrayWithArray:sectionDic[@"exams"]];
    if (sectionArray.count == 1) {
        //只有一个
        [tempArray removeObjectAtIndex:section];
    }else{
        [sectionArray removeObjectAtIndex:row];
        [sectionDic setValue:sectionArray forKey:@"exams"];
        [tempArray replaceObjectAtIndex:section withObject:sectionDic];
    }
    
    self.dataArray = tempArray;
}

//添加-某基线下的项目、评估、宣教 --- 暂时废除
-(void)addTaskAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger section = indexPath.section;
//    NSInteger row = indexPath.row;
    NSMutableArray *tempArray = [NSMutableArray arrayWithArray:self.dataArray];
    NSMutableDictionary *sectionDic = [NSMutableDictionary dictionaryWithDictionary:tempArray[section]];
    NSMutableArray *sectionArray = [NSMutableArray arrayWithArray:sectionDic[@"exams"]];
    [sectionArray addObject:@{@"codes":@{@"codes":@[],@"names":@[]},@"preachcodes":@{@"codes":@[],@"names":@[]},@"evacodes":@{@"codes":@[],@"names":@[]},@"keywords":@[]}];
    [sectionDic setValue:sectionArray forKey:@"exams"];
    [tempArray replaceObjectAtIndex:section withObject:sectionDic];
    self.dataArray = tempArray;
}

//保存
-(void)bottomButtonTouched{
    for (NSDictionary *planDic in self.dataArray) {
        if (LGNSString(planDic[@"days"]).length == 0) {
            [TooltipView showMessage:@"请输入基线天数" offset:0];
            return;
        }
        if (LGNSString(planDic[@"task_type_name"]).length == 0) {
            [TooltipView showMessage:@"请选择随访类型" offset:0];
            return;
        }
        NSArray *examArray = planDic[@"exams"];
        for (NSDictionary *projectDic in examArray) {
            NSArray *projectArray = projectDic[@"codes"][@"codes"];
            if (projectArray.count == 0) {
                [TooltipView showMessage:@"请选择随访项目" offset:0];
                return;
            }
        }
    }
    
    if (self.planEditSuccess) {
        self.planEditSuccess(self.dataArray);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
//    NSArray *tempArray = self.dataArray[section][@"exams"];
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGLungFollowEditTaskCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.dataArray[indexPath.section];
    cell.indexPath = indexPath;
    cell.delegate = self;
//    cell.candelete = (self.dataArray.count == 1 && tempArray.count == 1 && indexPath.row == 0)?NO:YES;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(180);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LGLungFollowEditTaskHeader *headView = [[LGLungFollowEditTaskHeader alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(180))];
    headView.index = section+1;
    headView.dataDic = self.dataArray[section];
    headView.delegate = self;
    headView.candelete = (self.dataArray.count == 1 && section == 0)?NO:YES;
    return headView;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.view endEditing:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return viewPix(15);
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    LGCornerFooterView *footView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(15))];
    footView.bottomMargin = viewPix(8);
    return footView;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight-viewPix(65)-bottomSafeBarHeight/4.0) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGLungFollowEditTaskCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [self tableFooterView];
    }
    return _tableView;
}

-(UIView *)tableFooterView{
    UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(50))];
    UIButton *addButton = [UIButton buttonWithTitle:@"  新增随访计划" titleFont:LGFont(17) textColor:kThemeColor imageName:@"themeAdd12"];
    addButton.frame = CGRectMake(viewPix(16), viewPix(5), Screen_W-viewPix(32), viewPix(40));
    [addButton addBorder:kThemeColor lineWidth:1 cornerRidus:viewPix(20)];
    [addButton addTarget:self action:@selector(addFollowUpAction) forControlEvents:UIControlEventTouchUpInside];
    [baseView addSubview:addButton];
    return baseView;
}

-(LGBottomButtonView *)bottomView{
    if(!_bottomView){
        _bottomView = [[LGBottomButtonView alloc] initWithTitle:@"保存" color:@"#5E8EF5" type:LGBottomButtonTypeBack frame:CGRectMake(0, kMaxY(self.tableView.frame), Screen_W, viewPix(65)+bottomSafeBarHeight/4.0)];
        [_bottomView setShadoWithColor:@"#EFEFEF" offset:CGSizeMake(0, 2) opacity:1 radius:0];
        _bottomView.delegate = self;
    }
    return _bottomView;
}

@end
